
package projectx.core.common.element;

import java.util.regex.Pattern;

import projectx.core.base.ProjectXMessage;
import projectx.util.StringUtil;


public class SpaElement extends Element {

	//--------------------------------------------------------------------------
	//   S T A T I C M E M B E R S
	//
	//--------------------------------------------------------------------------
	private static final Pattern _patternAlnum = Pattern.compile( "[a-zA-Z0-9]*" );

	private static final Pattern PATTERN_UNSINGED_LONG = Pattern
			.compile("[0-9]*");

	private static final Pattern PATTERN_ALNUMCOMMA = Pattern
			.compile("[a-zA-Z0-9,]*");
	
	//--------------------------------------------------------------------------
	//   U T I L I T Y M E T H O D S
	//
	//--------------------------------------------------------------------------

	public static ProjectXMessage checkAsciiInteger(String name, String value,
			long min, long max) {

		if (Element.getLength(value) == 0) {
			return null;
		}

		ProjectXMessage ret = Element.checkAsciiInteger(name, value);
		if (ret != null) {
			return ret;
		}

		long intValue = Long.valueOf(value).longValue();

		String minStrVal = StringUtil.formatLong(min);
		String maxStrVal = StringUtil.formatLong(max);
		
		if (intValue < min) {
			ret = new ProjectXMessage("E0350", name, minStrVal, maxStrVal);
		}

		if (intValue > max) {
			ret = new ProjectXMessage("E0350", name, minStrVal, maxStrVal);
		}
		
		return ret;
	}

	public static ProjectXMessage checkAsciiUnSignedLong(String name,
			String value, long min, long max) {

		if (Element.getLength(value) == 0) {
			return null;
		}
		ProjectXMessage ret = PATTERN_UNSINGED_LONG.matcher(value).matches() ? null
				: new ProjectXMessage("E0332", name);

		if (ret != null) {
			return ret;
		}

		long longValue = Long.valueOf(value).longValue();

		String minStrVal = StringUtil.formatLong(min);
		String maxStrVal = StringUtil.formatLong(max);

		if (longValue < min) {
			ret = new ProjectXMessage("E0350", name, minStrVal, maxStrVal);
		}

		if (longValue > max) {
			ret = new ProjectXMessage("E0350", name, minStrVal, maxStrVal);
		}

		return ret;
	}

	public static ProjectXMessage checkAsciiDecimal(String name, String value,
			double min, double max) {

		if (Element.getLength(value) == 0) {
			return null;
		}

		ProjectXMessage ret = Element.checkAsciiDecimal(name, value);
		if (ret != null) {
			return ret;
		}

		double doubleValue = Double.valueOf(value).doubleValue();
		
		String minStrVal = StringUtil.formatDecimal(min);
		String maxStrVal = StringUtil.formatDecimal(max);

		if (doubleValue < min) {
			ret = new ProjectXMessage("E0350", name, minStrVal, maxStrVal);
		}

		if (doubleValue > max) {
			ret = new ProjectXMessage("E0350", name, minStrVal, maxStrVal);
		}

		return ret;

	}

	public static ProjectXMessage checkAscii(String name, String value, int min,
			int max) {

		if (Element.getLength(value) == 0) {
			return null;
		}

		ProjectXMessage ret = Element.checkAscii(name, value, max, false);

		StringBuffer sb = new StringBuffer();
		sb.append(min);
		sb.append("-");
		sb.append(max);

		if (ret != null) {
			return ret;
		}

		ret = checkMinLength(name, value, min);

		if (ret != null) {
			return new ProjectXMessage("E0320", name, sb.toString());
		}

		return null;
	}
	
    public static ProjectXMessage checkAsciiAlphaNumeric( String name, String value){

        if ( getLength( value ) == 0 ) return null;

        ProjectXMessage    ret = _patternAlnum.matcher( value ).matches() ? null : new ProjectXMessage( "E0331", name );

        return ret;
    }

	public static ProjectXMessage checkAsciiAlphaNumeric(String name,
			String value, int min, int max) {

		if (Element.getLength(value) == 0) {
			return null;
		}

		ProjectXMessage ret = Element.checkAsciiAlphaNumeric(name, value, max,
				false);

		if (ret != null) {
			return ret;
		}

		ret = checkMinLength(name, value, min);

		if (ret != null) {
			return new ProjectXMessage("E0322", name, String.valueOf(min),
					String.valueOf(max));
		}

		return null;
	}

	public static ProjectXMessage checkMinLength(String name, String value,
			int min) {

		if (Element.getByteLength(value) < min) {
			return new ProjectXMessage("E0321", name, new Integer(min));
		}

		return null;
	}


	public static ProjectXMessage checkFromToAsciiNumeric(String name,
			int maxLength, boolean fixedLength, String fromValue, String toValue) {

		ProjectXMessage ret = Element.checkAsciiNumeric(name, fromValue,
				maxLength, fixedLength);
		if (ret != null) {
			return ret;
		}

		ret = Element.checkAsciiNumeric(name, toValue, maxLength, fixedLength);
		if (ret != null) {
			return ret;
		}

		ret = Element.checkFromTo(name, fromValue, toValue);
		if (ret != null) {
			return ret;
		}

		return null;
	}

	public static ProjectXMessage checkFromToAsciiAlphaNumeric(String name,
			int maxLength, String fromValue, String toValue) {

		ProjectXMessage ret = Element.checkAsciiAlphaNumeric(name, fromValue,
				maxLength, true);
		if (ret != null) {
			return ret;
		}

		ret = Element.checkAsciiAlphaNumeric(name, toValue, maxLength, true);
		if (ret != null) {
			return ret;
		}

		ret = Element.checkFromTo(name, fromValue, toValue);
		if (ret != null) {
			return ret;
		}

		return null;
	}


	public static ProjectXMessage checkFromToAsciiAlphaNumeric(String name,
			int minLength, int maxLength, String fromValue, String toValue) {

		ProjectXMessage ret = SpaElement.checkAsciiAlphaNumeric(name, fromValue,
				minLength, maxLength);
		if (ret != null) {
			return ret;
		}

		ret = SpaElement.checkAsciiAlphaNumeric(name, toValue, minLength,
				maxLength);
		if (ret != null) {
			return ret;
		}

		ret = Element.checkFromTo(name, fromValue, toValue);
		if (ret != null) {
			return ret;
		}

		return null;
	}

	public static ProjectXMessage checkFromToAsciiUnSignedLong(String name,
			String fromValue, String toValue, long min, long max) {

		if (getLength(fromValue) == 0 && getLength(toValue) == 0) {
			return null;
		}

		ProjectXMessage ret = SpaElement.checkAsciiUnSignedLong(name, fromValue,
				min, max);
		if (ret != null) {
			return ret;
		}

		ret = SpaElement.checkAsciiUnSignedLong(name, toValue, min, max);
		if (ret != null) {
			return ret;
		}

		ret = checkFromTo(name, fromValue, toValue);

		return ret;
	}

	public static ProjectXMessage checkAsciiStarAlphaNumeric(String name,
			String value, int len, boolean fixedLength) {

		if (getLength(value) == 0){
			return null;
		}

		if (value.equals("*")) {
			return null;
		}
		ProjectXMessage ret = checkAsciiAlphaNumeric(name, value);
		if (ret != null) {
			return new ProjectXMessage("E0327", name);
		}
		ret = checkLength(name, value, len, fixedLength);
		if (ret != null) {
			if (fixedLength) {
				return new ProjectXMessage("E0323", name, String.valueOf(len));
			} else {
				return new ProjectXMessage("E0321", name, String.valueOf(len));
			}
		}

		return null;
	}

	public static ProjectXMessage checkAsciiAlphaNumericComma(String name,
			String value, int len, boolean fixedLength) {

		if (Element.getLength(value) == 0){
			return null;
		}

		ProjectXMessage ret = PATTERN_ALNUMCOMMA.matcher(value).matches() ? null
				: new ProjectXMessage("E0326", name);

		if (ret != null) {
			return ret;
		}

		return Element.checkLength(name, value, len, fixedLength);
	}
}
